﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：UnitOfWorkHelper.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Application.Services;
using TongTongMall.Domain.Repositories.Repositories;

namespace TongTongMall.Domain.Uow
{
    internal static class UnitOfWorkHelper
    {
      /// <summary>
      /// 判断当前类型是否能够使用unitofwork钩子进行注入
      /// </summary>
      /// <param name="type"></param>
      /// <returns></returns>
        public static bool IsConventionalUowClass(Type type)
        {
            return typeof(IRepository).IsAssignableFrom(type) || typeof(IApplicationService).IsAssignableFrom(type);
        }

        /// <summary>
        /// 判断当前类型是否能够使用unitofwork钩子进行注入
        /// </summary>
        /// <param name="methodInfo"></param>
        public static bool HasUnitOfWorkAttribute(MemberInfo methodInfo)
        {
            return methodInfo.IsDefined(typeof(UnitOfWorkAttribute), true);
        }

        /// <summary>
        /// 返回类型中若包含unitofworkattribute属性时对应的配置参数
        /// </summary>
        /// <param name="methodInfo"></param>
        public static UnitOfWorkAttribute GetUnitOfWorkAttributeOrNull(MemberInfo methodInfo)
        {
            var attrs = methodInfo.GetCustomAttributes(typeof(UnitOfWorkAttribute), false);
            if (attrs.Length <= 0)
            {
                return null;
            }

            return (UnitOfWorkAttribute)attrs[0];
        }
    }
}
